package by.amakarevich.services.impl;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.YarnDao;
import by.amakarevich.dataaccess.ProductDao;
import by.amakarevich.datamodel.Yarn;
import by.amakarevich.datamodel.Product;
import by.amakarevich.services.YarnService;

@Service
public class YarnServiceImpl implements YarnService {

	@Inject
	private YarnDao yarnDao;
	
	@Inject
	private ProductDao productDao;
	
	@Override
	public void createNewYarn(Yarn yarn,  Product product) {
		Validate.isTrue(yarn.getId() == null, "This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		yarn.setProduct(product);
		yarnDao.insert(yarn);
	}

	@Override
	public Yarn get(Long id) {
		return yarnDao.getById(id);
	}

	@Override
	public void updateYarn(Yarn yarn) {
		yarnDao.update(yarn);
	}

	@Override
	public void removeYarn(Long id) {

		yarnDao.delete(id);
		productDao.delete(id);

	}
	  @Override
	    public void deleteAll() {
		  yarnDao.deleteAll();
		  productDao.deleteAll();
	    }
}
